package com.b2b.util;

import com.b2b.mapper.ShopMapper;
import com.b2b.model.ShopDto;
import com.b2b.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ShopUtil {
    private final ShopMapper shopMapper;
    public final ShopService shopService;
    @Autowired
    public ShopUtil (ShopMapper shopMapper,ShopService shopService){

        this.shopMapper = shopMapper;
        this.shopService = shopService;
    }

    public Integer getCompanyId(Integer shopId){
        ShopDto shop = shopService.selectByPrimaryKey(shopId);
        Integer companyId = shop.getCompanyId();
        return companyId;
    }

    public String getShopName(Integer shopId){
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(shopId);
        shopDto = shopService.selectByPrimaryKey(shopDto);
        String shopName = shopDto.getShopName();
        return shopName;
    }
    public List<ShopDto> getAllShop(){
        List<ShopDto> shopList = shopService.selectAll();

        return shopList;
    }
    public List<ShopDto> getShopByState(char state){
        ShopDto shopDto = new ShopDto();
        shopDto.setState(state);
        List<ShopDto> shopList = shopService.select(shopDto);

        return shopList;
    }
    public Integer updateShopState(ShopDto shopDto){
        Integer count = shopService.updateByPrimaryKeySelective(shopDto);
        return count;
    }
    public ShopDto getShopByName(String shopName){
        ShopDto shop = new ShopDto();
        shop.setShopName(shopName);
        shop = shopService.selectOne(shop);
        return shop;
    }
    public ShopDto getShopById(Integer shopId){
        ShopDto shopDto = shopService.selectByPrimaryKey(shopId);
        return shopDto;
    }

    public List<Integer> getShopIdList(Integer companyId){
        ShopDto shop = new ShopDto();
        shop.setCompanyId(companyId);
        List<ShopDto> shops = shopService.select(shop);
        List<Integer> shopIds = new ArrayList<>();
        for (int i = 0; i < shops.size(); i++) {
            Integer shopId = shops.get(i).getShopId();
            shopIds.add(shopId);
        }
        return shopIds;
    }
}
